//
//  HXLWeatherViewController.m
//  On The Load
//
//  Created by 晓琳 on 15/11/6.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "HXLWeatherViewController.h"
#import "TravelPlan.h"
#import "BaseTableView.h"
#import "HXLWeatherTableViewCell.h"
#import "AppTools.h"
#import "WeekModel.h"
#import <MJRefresh.h>
#import "MBProgressHUD+show.h"
@interface HXLWeatherViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *cityNameEnArray;
@property (nonatomic, strong) BaseTableView *weatherTV;
@property (nonatomic, strong) NSMutableArray *weekArray;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NSMutableDictionary *finDic;
@property (nonatomic, strong) MBProgressHUD *hub;
@end

@implementation HXLWeatherViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    if (self.cityArray.count != 0) {
        
        self.hub = [[MBProgressHUD alloc] init];
        self.hub.labelText = @"正在为您加载数据";
        [self.view addSubview:self.hub];
        [self.hub show:YES];
        
    }else {
        [MBProgressHUD showInfoWithText:@"请先添加行程"];
        
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.cityNameEnArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    self.weekArray = [NSMutableArray array];
    self.finDic = [NSMutableDictionary dictionary];
    UIColor *backColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"back1.png"]];
    [self.view setBackgroundColor:backColor];
    if (self.cityArray.count != 0) {
        
        
      for (TravelPlan *plan in self.cityArray) {
          NSLog(@"city---%@---%@", plan.cityNameEn, plan.cityName);
          [self.cityNameEnArray addObject:plan.cityNameEn];
      }
    }
    self.weatherTV = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0*SCALEW, WIDTH, HEIGHT)style:UITableViewStyleGrouped];
    self.weatherTV.delegate = self;
    self.weatherTV.dataSource = self;
    self.weatherTV.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.weatherTV];
    
    //下拉刷新
    [self getAfnData];
}

- (void)setCityArray:(NSArray *)cityArray{
    _cityArray = cityArray;
    
    for (TravelPlan *model in _cityArray) {
        NSLog(@"fff%@", model.cityName);
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40*SCALEH;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    NSLog(@"ddd = %ld",self.titleArray.count)
    return self.titleArray.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    NSLog(@"ssss = %ld",self.titleArray.count);
    return [self.titleArray objectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *cellIdentifer = @"weather";
    NSString *cellIdentifer = [NSString stringWithFormat:@"weather%ld", indexPath.section];
    HXLWeatherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (!cell) {
        cell = [[HXLWeatherTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
    }
    NSString *name = [self.cityNameEnArray objectAtIndex:indexPath.section];
//    NSLog(@"index = %ld",indexPath.section);

    NSMutableArray *array = [self.finDic objectForKey:name];
//    NSLog(@"%@", array);
    cell.weekArray = array;
    
    return cell;
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 420;
}


- (void) getAfnData {
    
    NSLog(@"222222 = %ld",self.cityNameEnArray.count);
    
    for (int i = 0; i < self.cityNameEnArray.count; i++) {
        NSString *localCity = [self.cityNameEnArray objectAtIndex:i];
    
      NSString *urlStr = [NSString stringWithFormat:@"http://api.k780.com:88/?app=weather.future&weaid=%@&&appkey=15444&sign=0ae11d1a1780cbd0d768f016c19a8a1c&format=json",localCity];
//        NSLog(@"path = %@",urlStr);
    
     [AppTools getAFN:urlStr andpara:nil andBlock:^(id result) {

          NSDictionary *bigDic = result;
          NSMutableArray *array = [bigDic objectForKey:@"result"];
         NSMutableArray *weekArray = [NSMutableArray array];
          for (NSDictionary *dic in array) {
              WeekModel *seven = [[WeekModel alloc] init];
              [seven setValuesForKeysWithDictionary:dic];
              [weekArray addObject:seven];
        }
         
//         NSLog(@"week = %ld",self.weekArray.count);
         
         WeekModel *model = [weekArray lastObject];
         NSString *name = model.citynm;
         [self.titleArray addObject:name];
         
         
         [self.finDic setObject:weekArray forKey:localCity];
         [self.weatherTV reloadData];
         [self.hub hide:YES];
//         [self.weatherTV.header endRefreshing];
     
      } andfailBlock:^(id result) {
        
        
      }];
    }
    
}

- (void) addHeaderRefresh {
    //下拉刷新
    [self.weekArray removeAllObjects];
    [self.finDic removeAllObjects];
//    [self.cityNameEnArray removeAllObjects];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getAfnData)];
    [header beginRefreshing];
    self.weatherTV.header = header;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
